package hu.diploma.android.serviceimpl;

import hu.diploma.common.service.SmsMmsService;
import hu.diploma.common.vos.SmsMmsVo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.provider.BaseColumns;
import android.util.Log;

public class SmsMmsServiceImpl implements SmsMmsService {

	private final ContentResolver contentResolver;

	public SmsMmsServiceImpl(ContentResolver contentResolver) {
		super();
		this.contentResolver = contentResolver;
	}

	@Override
	public SmsMmsVo save(SmsMmsVo vo) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public SmsMmsVo merge(SmsMmsVo vo) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public SmsMmsVo findById(Serializable searchId) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public SmsMmsVo findByPhoneId(Serializable searchId) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<SmsMmsVo> loadAll() throws Exception {

		Uri uriSMS = Uri.parse("content://sms");
		Cursor c = contentResolver.query(uriSMS, null, null, null, null);

		ArrayList<SmsMmsVo> smsMmsVos = new ArrayList<SmsMmsVo>();
		try {

			while (c.moveToNext()) {

				Integer id = c.getInt(c.getColumnIndex(BaseColumns._ID));
				String number = c.getString(c.getColumnIndexOrThrow("address"));
				String body = c.getString(c.getColumnIndexOrThrow("body"));
				String date = c.getString(c.getColumnIndexOrThrow("date"));

				SmsMmsVo smsMmsVo = new SmsMmsVo();
				smsMmsVo.setPhoneId(id.longValue());
				smsMmsVo.setBody(body);
				smsMmsVo.setNumber(number);
				smsMmsVo.setDate(new Date(Long.parseLong(date)));

				Log.d("DEBUG", "smsMmsVo" + smsMmsVo);
				smsMmsVos.add(smsMmsVo);

			}
		} finally {
			c.close();
		}

		return smsMmsVos;
	}

	@Override
	public void delete(SmsMmsVo vo) throws Exception {
		// TODO Auto-generated method stub

	}

}
